package com.example.sls.dao.logistics;

import com.example.sls.pojo.logistics.Equipment;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 设备表(Equipment)表数据库访问层
 *
 * @author makejava
 * @since 2024-06-24 08:24:23
 */
public interface EquipmentDao {
  /**
   * 通过ID查询单条数据
   *
   * @param id 主键
   * @return 实例对象
   */
  @Select("select * from equipment where id=#{id}")
  Equipment queryById(Integer id);

  /**
   * 查询指定行数据
   *
   * @param equipment 查询条件
   * @param pageSize  分页对象
   * @param offset    偏移量，起始处
   * @return 对象列表
   */
  @Select("select * from equipment where 1=1" +
          " and (id=#{equipment.id} or (isnull(#{equipment.id}) or #{equipment.id}=0))" +
          " and (name=#{equipment.name} or (isnull(#{equipment.name}) or #{equipment.name}=''))" +
          " and (status=#{equipment.status} or (isnull(#{equipment.status}) or #{equipment.status}=''))" +
          " and (location=#{equipment.location} or (isnull(#{equipment.location}) or #{equipment.location}=''))" +
          " and (created_at=#{equipment.createdAt} or (isnull(#{equipment.createdAt}) or #{equipment.createdAt}=''))" +
          " limit #{pageSize} offset #{offset}")
  List<Equipment> queryAllByLimit(Equipment equipment, Integer pageSize, Integer offset);

  /**
   * 统计总行数
   *
   * @return 总行数
   */
  @Select("select count(*) from equipment")
  long count();

  /**
   * 新增数据
   *
   * @param equipment 实例对象
   * @return 影响行数
   */
  @Insert("insert into equipment (id,name,status,location,created_at) " +
          "values(" +
          "if(#{id}!=0 and !isnull(#{id}),#{id},null)," +
          "if(!isnull(#{name}),#{name},null)," +
          "if(!isnull(#{status}),#{status},null)," +
          "if(!isnull(#{location}),#{location},null)," +
          "if(!isnull(#{createdAt}),#{createdAt},null)" +
          ")")
  int insert(Equipment equipment);

  /**
   * 通过id修改数据
   * id 主键
   *
   * @param equipment 实例对象
   * @return 影响行数
   */
  @Update("update equipment set " +
          "name=if(isnull(#{name}),name,#{name})," +
          "status=if(isnull(#{status}),status,#{status})," +
          "location=if(isnull(#{location}),location,#{location})," +
          "created_at=if(isnull(#{createdAt}),created_at,#{createdAt})" +
          " where id=#{id}")
  int update(Equipment equipment);

  /**
   * 通过主键删除数据
   *
   * @param id 主键
   * @return 影响行数
   */
  @Delete("delete from equipment where id=#{id}")
  int deleteById(Integer id);
}

